﻿using System;
using QFramework;

namespace ProjectSurvival
{
    public class CoinUpgradeItem
    {
        public string Key { get;private set; }
        public string Description { get; private set; }

        public int Price { get; private set; }

        //依赖条件：满足条件后才能继续下一级的升级
        private Func<CoinUpgradeItem, bool> mCondition;
        //是否升级完成
        public bool UpgradeFinish { get; set; }

        public EasyEvent OnItemUpgrandeChanged = new EasyEvent();
        private CoinUpgradeItem _next = null;
        public CoinUpgradeItem Next(CoinUpgradeItem next)
        {
            _next = next;
            _next.Condition(_ => UpgradeFinish);
            return _next;
        }
        

        //用金币升级属性
        public void Upgrade()
        {
            mOnUpgrade?.Invoke(this);
            UpgradeFinish = true;
            //触发金币升级系统的升级状态事件，用于保存升级状态到本地
            CoinUpgradeSystem.OnUpgradeStateChanged.Trigger();
            //触发金币升级属性的状态事件，用于更新是否显示
            TriggerOnChanged();
        }

        public void TriggerOnChanged()
        {
            OnItemUpgrandeChanged.Trigger();
            _next?.TriggerOnChanged();
        }
        private Action<CoinUpgradeItem> mOnUpgrade;

        public CoinUpgradeItem WithKey(string key)
        {
            Key = key;
            return this;
        }

        public CoinUpgradeItem WithDescription(string description)
        {
            Description = description;
            return this;
        }

        public CoinUpgradeItem WithPrice(int price)
        {
            Price = price;
            return this;
        }

        public CoinUpgradeItem OnUpgrade(Action<CoinUpgradeItem> onUpgrade)
        {
            mOnUpgrade += onUpgrade;
            return this;
        }

        public CoinUpgradeItem Condition(Func<CoinUpgradeItem, bool> condition)
        {
            mCondition = condition;
            return this;
        }

        //检查是否满足条件
        public bool ConditionCheck()
        {
            if (mCondition != null)
            {
                return !UpgradeFinish&&mCondition.Invoke(this);
            }

            return !UpgradeFinish;
        }
    }
}